﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    public enum SqlServerAuthenticationMode
    {
        EmptyValue,
        SqlServer = 1,
        NTAuthentication = 2
    }

    internal class ServerDocument : Document
    {
        #region internal
        //Constructors
        internal ServerDocument(SqlServer.Server server)
        {
            DocumentType = DocumentType.Server;
            _server = server;
            RootServer = _server;
            URL = "index.html";
        }
        //Properties
        internal override string DisplayName
        { 
            get { 
                return _server.Name + " (SQL Server " + _server.Version + ")";
            } 
        }
        internal IEnumerable<DatabaseDocument> databaseDocuments
        {
            get
            {
                for (int index = 0; index < _databaseDocuments.Count; index++)
                {
                    yield return _databaseDocuments[index];
                }
            }
        }
        //Methods
        internal void Connect()
        {
            _server.Connect();
        }
        internal void LoadDatabaseList()
        {
            _server.LoadDatabaseList();
            foreach (SqlServer.Database database in _server.Databases)
            {
                DatabaseDocument databaseDocument = new DatabaseDocument(database);
                _databaseDocuments.Add(databaseDocument);
            }
        }
        internal override string GetHtml()
        {
            Invalidate();

            AddCssLine(@"      	.propertyColumn {width: 300px; word-wrap: break-word;}");

            AddBodyLine(@"<h2 id=""title"">" + _server.Name + @" (Server)</h2>");
            AddBodyLine(@"<div id=""mainBody"">");
            AddBodyLine(@"<table>");
            AddBodyLine(@"<colgroup><col class=""propertyColumn""><col class=""valueColumn""></colgroup>");
            AddBodyLine(@"<tr><th>Property</th><th>Value</th></tr>");
            AddBodyLine(@"<tr class=""evenRow""><td>Name</td><td>" + ScrubHtml(_server.Name) + @"</td></tr>");
            AddBodyLine(@"<tr class=""oddRow""><td>Version</td><td>" + ScrubHtml(_server.Version) + @"</td></tr>");

            if (_server.Version.Substring(0, 1) == "8")
                AddBodyLine(@"<tr class=""evenRow""><td>Product</td><td>" + ScrubHtml(_server.Product) + @" 2000</td></tr>");
            else if (_server.Version.Substring(0, 1) == "9")
                AddBodyLine(@"<tr class=""evenRow""><td>Product</td><td>" + ScrubHtml(_server.Product) + @" 2005</td></tr>");
            else if (_server.Version.Substring(0, 2) == "10")
                AddBodyLine(@"<tr class=""evenRow""><td>Product</td><td>" + ScrubHtml(_server.Product) + @" 2008</td></tr>");

            if (_server.OperatingSystem.IndexOf("4.0") != -1)
                AddBodyLine(@"<tr class=""oddRow""><td>Operating System</td><td>Windows 95 " + ScrubHtml(_server.OperatingSystem) + @"</td></tr>");
            else if (_server.OperatingSystem.IndexOf("4.10") != -1)
                AddBodyLine(@"<tr class=""oddRow""><td>Operating System</td><td>Windows 98 " + ScrubHtml(_server.OperatingSystem) + @"</td></tr>");
            else if (_server.OperatingSystem.IndexOf("4.90") != -1)
                AddBodyLine(@"<tr class=""oddRow""><td>Operating System</td><td>Windows ME " + ScrubHtml(_server.OperatingSystem) + @"</td></tr>");
            else if (_server.OperatingSystem.IndexOf("5.0") != -1)
                AddBodyLine(@"<tr class=""oddRow""><td>Operating System</td><td>Windows 2000 " + ScrubHtml(_server.OperatingSystem) + @"</td></tr>");
            else if (_server.OperatingSystem.IndexOf("5.1") != -1)
                AddBodyLine(@"<tr class=""oddRow""><td>Operating System</td><td>Windows XP " + ScrubHtml(_server.OperatingSystem) + @"</td></tr>");
            else if (_server.OperatingSystem.IndexOf("5.2") != -1)
                AddBodyLine(@"<tr class=""oddRow""><td>Operating System</td><td>Windows Server 2003 " + ScrubHtml(_server.OperatingSystem) + @"</td></tr>");
            else if (_server.OperatingSystem.IndexOf("6.0") != -1)
                AddBodyLine(@"<tr class=""oddRow""><td>Operating System</td><td>Windows Vista " + ScrubHtml(_server.OperatingSystem) + @"</td></tr>");
            else
                AddBodyLine(@"<tr class=""oddRow""><td>Operating System</td><td>" + ScrubHtml(_server.OperatingSystem) + @"</td></tr>");

            AddBodyLine(@"<tr class=""evenRow""><td>Platform</td><td>" + ScrubHtml(_server.Platform) + @"</td></tr>");
            AddBodyLine(@"<tr class=""oddRow""><td>Language</td><td>" + ScrubHtml(_server.Language) + @"</td></tr>");
            AddBodyLine(@"<tr class=""evenRow""><td>Memory Size</td><td>" + AddCommasToNumericString(_server.Memory.ToString())  + @" MB</td></tr>");
            AddBodyLine(@"<tr class=""oddRow""><td>Number of Processors</td><td>" + _server.NumProcessors.ToString() + @"</td></tr>");
            AddBodyLine(@"<tr class=""evenRow""><td>Root Directory</td><td>" + ScrubHtml(_server.RootDirectory) + @"</td></tr>");
            AddBodyLine(@"<tr class=""oddRow""><td>Collation Name</td><td>" + ScrubHtml(_server.CollationName) + @"</td></tr>");
            AddBodyLine(@"<tr class=""evenRow""><td>Is Clustered</td><td>" + _server.IsClustered.ToString() + @"</td></tr>");
            AddBodyLine(@"<tr class=""oddRow""><td>Allow Sql Authentication</td><td>" + _server.AllowSqlAuthentication.ToString() + @"</td></tr>");

            AddBodyLine(@"<tr class=""evenRow""><td>Audit Level</td><td>" + ScrubHtml(_server.AuditLevel) + @"</td></tr>");
            AddBodyLine(@"<tr class=""oddRow""><td>Enable C2 Auditing</td><td>" + _server.EnableC2Auditing.ToString() + @"</td></tr>");
            AddBodyLine(@"<tr class=""evenRow""><td>Allow Nested Triggers</td><td>" + _server.AllowTriggersToFireTriggers.ToString() + @"</td></tr>");
            AddBodyLine(@"<tr class=""oddRow""><td>Default Language</td><td>" + ScrubHtml(_server.DefaultLanguage) + @"</td></tr>");
            AddBodyLine(@"<tr class=""evenRow""><td>Scan Stored Procedures at Start Up</td><td>" + _server.ScanStoredProcsAtStartup.ToString() + @"</td></tr>");

            AddBodyLine(@"<tr class=""oddRow""><td>Two Digit Cutoff</td><td>" + _server.TwoDigitCutoff.ToString() + @"</td></tr>");
            AddBodyLine(@"<tr class=""evenRow""><td>Network Packet Size</td><td>" + AddCommasToNumericString(_server.NetworkPacketSize.ToString()) + @" Bytes</td></tr>");
            AddBodyLine(@"<tr class=""oddRow""><td>Remote Login Timeout</td><td>" + _server.RemoteLoginTimeout.ToString() + @" Seconds</td></tr>");
            AddBodyLine(@"<tr class=""evenRow""><td>Remote Query Timeout</td><td>" + _server.RemoteQueryTimeout.ToString() + @" Seconds</td></tr>");
            
            if (_server.MaxNumberOfLocks == 0)
                AddBodyLine(@"<tr class=""oddRow""><td>Max Number of Locks</td><td>Unlimited</td></tr>");
            else
                AddBodyLine(@"<tr class=""oddRow""><td>Max Number of Locks</td><td>" + _server.MaxNumberOfLocks.ToString() + @"</td></tr>");

            if (_server.QueryWait == -1)
                AddBodyLine(@"<tr class=""evenRow""><td>Query Wait</td><td>25 times the estimated query cost</td></tr>");
            else
                AddBodyLine(@"<tr class=""evenRow""><td>Query Wait</td><td>" + _server.QueryWait.ToString() + @" Seconds</td></tr>");

            AddBodyLine(@"</table>");
            AddBodyLine(@"</div>");

            return CreateHtmlDocument();
        }
        #endregion
        #region Private
        //Properties
        SqlServer.Server _server = null;
        List<DatabaseDocument> _databaseDocuments = new List<DatabaseDocument>();
        //Methods
        #endregion
    }
}
